package com.example.bitcoinconverter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {
  private EditText textprice;
  private EditText text;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    StrictMode.ThreadPolicy policy = new StrictMode.
    ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textprice = (EditText) findViewById(R.id.editText2);
    text = (EditText) findViewById(R.id.editText3);

  }

  // this method is called at button click because we assigned the name to the
  // "OnClick property" of the button
  public void onClick(View view) {
    switch (view.getId()) {
    case R.id.button1:
      String currency = textprice.getText().toString();
      if (currency != "PLN" && currency != "GPB" && currency != "EUR" && currency != "USD" && currency != "CHF") {
          Toast.makeText(this, "Please enter a valid currency: PLN, GPB, EUR, USD, CHF",
              Toast.LENGTH_LONG).show();
          return;
      }
      if (text.getText().length() == 0) {
        Toast.makeText(this, "Please enter a valid number for amount",
            Toast.LENGTH_LONG).show();
        return;
      }
      String amount = text.getText().toString();
      String address = "http://blockchain.info/pl/tobtc?currency=" + currency + "&value=" + amount;
      try {
          URL url = new URL(address);
          HttpURLConnection con = (HttpURLConnection) url.openConnection();
          readStream(con.getInputStream());
          } catch (Exception e) {
          e.printStackTrace();
        }
      break;
    }
  }

  private void readStream(InputStream in) {
      BufferedReader reader = null;
      try {
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while ((line = reader.readLine()) != null) {
            Toast.makeText(this, line, Toast.LENGTH_LONG).show();
        }
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (reader != null) {
          try {
            reader.close();
          } catch (IOException e) {
            e.printStackTrace();
            }
        }
      }
    }  
  
@Override
protected void onDestroy() {
	// TODO Auto-generated method stub
	Toast.makeText(this, "destroy", Toast.LENGTH_SHORT).show();
	super.onDestroy();
}

@Override
protected void onPause() {
	// TODO Auto-generated method stub
	Toast.makeText(this, "pause", Toast.LENGTH_SHORT).show();
	super.onPause();
}

@Override
protected void onRestart() {
	// TODO Auto-generated method stub
	Toast.makeText(this, "restart", Toast.LENGTH_SHORT).show();
	super.onRestart();
}

@Override
protected void onResume() {
	// TODO Auto-generated method stub
	Toast.makeText(this, "resume", Toast.LENGTH_SHORT).show();
	super.onResume();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
	// TODO Auto-generated method stub
	Toast.makeText(this, "saveinstance", Toast.LENGTH_SHORT).show();
	super.onSaveInstanceState(outState);
}

@Override
protected void onStart() {
	// TODO Auto-generated method stub
	Toast.makeText(this, "start", Toast.LENGTH_SHORT).show();
	super.onStart();
}

@Override
protected void onStop() {
	// TODO Auto-generated method stub
	Toast.makeText(this, "stop", Toast.LENGTH_SHORT).show();
	super.onStop();
}

} 
